import { ref, computed } from 'vue'
import { defineStore } from "pinia";
import api from "@/api/category"
import type { CategoryItem } from '@/config/types';


export const useCategotryStore = defineStore('category', () => {
    const loading = ref<boolean>(false)
    const categories = ref<CategoryItem[]>([])

    const updating = ref<boolean>(false)
    const parentItem = ref<CategoryItem | null>(null)
    const categoryItem = ref<CategoryItem>({
        id: 0,
        title: '',
        parentId: 0,
        sort: 0,
        children: null
    })
    const sort = computed<number>(() => {
        let idx = 0
        categories.value.map((value : CategoryItem) => {
            if (idx < value.sort) {
                idx = value.sort
            }
            value.children?.map((child : CategoryItem) => {
                if (idx < child.sort) {
                    idx = child.sort + 1
                }
            })
        })
        return idx + 1
    })

    async function loadCategories(reload = true) {
        if (categories.value.length > 0 && !reload) return
        loading.value = true
        try {
            const res = await api.getList()
            categories.value = res.data
            loading.value = false
        } catch(e) {
            loading.value = false
        }
    }

    async function loadParent(id : number) {
        updating.value = true
        parentItem.value = null
        if (categories.value.length == 0) {
            await loadCategories()
        }
        categories.value.map((value : CategoryItem) => {
            if (value.id === id) {
                parentItem.value = value
                return
            }
        })
        updating.value = false
    }

    async function loadCategory(id : number) {
        updating.value = true
        if (categories.value.length == 0) {
            await loadCategories()
        }
        categories.value.map((value : CategoryItem) => {
            if (value.id === id) {
                categoryItem.value = value
                parentItem.value = null
                return
            }
            value.children?.map((child : CategoryItem) => {
                if (child.id === id) {
                    categoryItem.value = child
                    loadParent(categoryItem.value.parentId)
                    return
                }
            })
        })
        updating.value = false
    }

    async function newCategory(parentId : number) {
        updating.value = true
        if (parentId > 0) {
            await loadParent(parentId)
        }
        categoryItem.value = {
            id: 0,
            title: '',
            parentId: parentId,
            sort: sort.value,
            children: null,
        }
        updating.value = false
    }

    async function saveCategory(item : CategoryItem) {
        updating.value = true
        try {
            let res
            if (item.id > 0) {
                res = await api.update(item.id, {
                    title: item.title,
                    parentId: item.parentId,
                    sort: item.sort
                })
            } else {
                res = await api.create({
                    title: item.title,
                    parentId: item.parentId,
                    sort: item.sort
                })
            }
            updating.value = false
            if (res.data) {
                await loadCategories()
            }
            return res.data
        } catch (e) {
            updating.value = false
        }
        return false
    }

    async function removeCategory(id : number) {
        loading.value = true
        try {
            const res = await api.delete(id)
            loading.value = false
            return res.data
        } catch(e) {
            loading.value = false
        }
        return false
    }

    return {
        loading,
        categories,
        loadCategories,

        updating,
        parentItem,
        categoryItem,
        loadParent,
        loadCategory,
        newCategory,
        saveCategory,
        removeCategory,
    }
})